class LoginState extends fsm.State {

    private textField: renderer.TextField;

    //11111111111111111111111111111
    begining: renderer.DisplayObject;
    //11111111111111111111111111111

    private onClick = (eventData: any) => {
        //this.createUser();
        gameFsm.replaceState(new PlayingState());
    }

    private createUser() {
        //user = new User();
        //user.level = 1;
        //user.name = 'Wander';
        missionManager.init();
    }

    onEnter() {

        //11111111111111111111111111111
        const image1 = new Image();
        image1.src = 'begining.jpg';
        const background = new renderer.Bitmap(0, 0, image1);
        this.begining = background;
        //11111111111111111111111111111

        this.textField = new renderer.TextField(100, 100);
        this.textField.text = '点击进入';

        stage.addChild(this.begining); //11111111111111111111111111111

        stage.addEventListener("click", this.onClick);
    }



    onExit() {
        stage.removeEventListener("click", this.onClick);
        // stage.removeChild(this.textField);
        stage.removeAllChildren();
    }
}

/**
 * 用户面板 UI 界面
 * MVC:View
 */
class UserPanelUI extends renderer.DisplayObjectContainer {

    constructor() {
        super();
        const userNameLabel = new renderer.TextField(0, 0);
        this.addChild(userNameLabel);
        const userLevelLabel = new renderer.TextField(50, 0);
        this.addChild(userLevelLabel);

        const userAttackLabel = new renderer.TextField(100, 0);
        this.addChild(userAttackLabel);

        // userLevelLabel.text = "lv:" + user.level;
        // userNameLabel.text = user.name;
        // userAttackLabel.text = "attack:" + user.attack;

        // user.addEventListener("userchange", () => {
        //     userLevelLabel.text = "lv:" + user.level;
        //     userNameLabel.text = user.name;
        //     userAttackLabel.text = "attack:" + user.attack;
        // })
    }
}

let dialogContainer: renderer.DisplayObjectContainer

class PlayingState extends fsm.State {

    onEnter() {
        const map = mapManager.getMap();
        const mapContainer = new renderer.DisplayObjectContainer();
        mapManager.initContainer(mapContainer);
        const missionPanel = new MissionPanel();
        const uicontainer = new renderer.DisplayObjectContainer();
        const userPanel = new UserPanelUI();
        dialogContainer = new renderer.DisplayObjectContainer();

        stage.addChild(mapContainer);
        stage.addChild(uicontainer)

        uicontainer.addChild(userPanel);
        uicontainer.addChild(missionPanel);
        uicontainer.addChild(dialogContainer);
        missionPanel.x = 250;
        missionPanel.y = 100;
        mapContainer.x = mapContainer.y = 50;

        mapManager.changeMap("map");

    }



    onUpdate() {

        //user.update();

    }
}



let pool = new command.CommandPool();
let user: User;

gameFsm.replaceState(new LoginState());

function loadConfig(url: string, callback: Function) {
    const loader = new XMLHttpRequest();
    loader.open("get", url);
    loader.send();
    loader.onload = () => {
        const obj = JSON.parse(loader.response);
        callback(obj);
    }
}

loadConfig("mission.json", (obj: any) => {
    missionManager.parseFromConfig(obj);
    loadConfig("npc.json", (obj: any) => {
        npcManager.parseFromConfig(obj);
        loadConfig("map.json", (obj: any) => {
            mapManager.parseFromConfig("map", obj)
            loadConfig("map1.json", (obj: any) => {
                mapManager.parseFromConfig("map1", obj)
            })
        })
    })
})